<?php
class News extends Base
{
    protected function getPrimaryKey()
    {
        return "newsId";
    }

    protected function getTableName()
    {
        return "news";
    }


    //显示最新新闻
    public function findLast($count)
    {
        $sql = "select * from news order by createDate desc limit $count";
        return $this->_db->getall($sql);
    }

    public function getTotalCount()
    {
        $sql = "select count(*) as count from " . $this->getTableName() . " where status<>'D'";
        $count = $this->_db->getOne($sql);
        return $count['count'];
    }

    public function insert($post)
    {
        $tableName = "news";
        return $this->_db->createRecord($tableName, $this->competeRecord($post)) > 0;
    }

    public function update($post)
    {
        $tableName = "news";
        $record = $this->competeRecord($post);
        $record['newsId'] = $post['newsId'];
        return $this->_db->updateRecord($tableName, $record, $this->getPrimaryKey()) > 0;
    }

    public function findByTypeId($count, $typeId)
    {
        $sql = "select * from news where `type` = $typeId limit $count";
        return $this->_db->getall($sql);
    }
    public function findRelatedCommentByUserId($userId){
        $sql = "select n.* from news n,comment c where n.newsId = c.operatorId and c.type='NEWS' and c.userId=$userId";
        return $this->_db->getall($sql);
    }

    protected function competeRecord($post)
    {
        $record = array(
            "title" => $post['title'],
            "content" => $post['content'],
            "createDate" => date("Y-m-d H:i:s"),
            "updateDate" => date("Y-m-d H:i:s"),
            "type" => $post['type'],
            "hits" => 0,
            "status" => 1,
            "tags" => $post['tags']
        );
        return $record;
    }


}

?>